package com.example.myapplication10;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<Person> contactList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView contactView = (ListView) findViewById(R.id.view_list);

        mesAdapter mesAdapter = new mesAdapter(MainActivity.this,R.layout.mes_show,contactList);

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
        }else {
            initInfo();
            mesAdapter.notifyDataSetChanged();
        }

        ListView listView =(ListView)findViewById(R.id.view_list);
        listView.setAdapter(mesAdapter);

//        点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Person person = contactList.get(i);
//                Toast.makeText(MainActivity.this,person.getName(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this,SendMessage.class);
                intent.putExtra("send_name",person.getName());
                intent.putExtra("send_number",person.getPhone());
                startActivity(intent);

            }
        });
    }
    private void initInfo(){
        Cursor cursor = null;
        try{
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
            if(cursor!= null){
                while (cursor.moveToNext()) {
//                    获取姓名手机号，填入Person类中
                    @SuppressLint("Range") String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Person p = new Person(displayName, number);
                    contactList.add(p);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(cursor != null){
                cursor.close();
            }
        }
    }
    //    requestPermissions后执行的函数，grantResults为授权结果
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults){
        switch (requestCode){
            case 1:
//                授权结果与PERMISSION_GRANTED比较，如果相同则已授权
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    initInfo();
                }else {
                    Toast.makeText(this,"You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}